<?php
/*
 * 商品
 */

namespace app\adminbackstage\controller;
use app\BaseController;
use think\facade\Request;
use app\adminbackstage\model\Product;
use think\facade\Config;

class ProductController extends BaseController{

    //商品列表
    public function productList(){
        $a_uid = Request::param('a_uid');
        $page = Request::param('page');
        $num = Request::param('num');
        
        $p_name = Request::param('p_name');
        $br_id = Request::param('br_id');
        $class_one = Request::param('class_one');
        $class_two = Request::param('class_two');
        $class_thr = Request::param('class_thr');
        $supplier = Request::param('supplier');
        $con_person = Request::param('con_person');
        $con_information = Request::param('con_information');
        $res_person = Request::param('res_person');
        $cost_price_min = Request::param('cost_price_min');
        $supply_price_min = Request::param('supply_price_min');
        $cost_price_max = Request::param('cost_price_max');
        $supply_price_max = Request::param('supply_price_max');
        
        $where = [];
        $where[] = ['p.enable','=','1'];
        if(!empty($p_name)){
            $where[] = ['p.p_name','like','%'.$p_name.'%'];
        }
        if(!empty($class_one)){
            $where[] = ['p.class_one','=',$class_one];
        }
        if(!empty($type)){
            $where[] = ['p.class_two','=',$class_two];
        }
        if(!empty($type)){
            $where[] = ['p.class_thr','=',$class_thr];
        }
        if(!empty($type)){
            $where[] = ['p.supplier','=',$supplier];
        }
        if(!empty($type)){
            $where[] = ['p.con_person','=',$con_person];
        }
        if(!empty($type)){
            $where[] = ['p.con_information','=',$con_information];
        }
        if(!empty($br_id)){
            $where[] = ['b.br_id','=',$br_id];
        }
        if(!empty($p_name)){
            $where[] = ['p.p_name','like','%'.$p_name.'%'];
        }
        if(!empty($cost_price_min) && !empty($cost_price_max)){
            $where[] = ['p.cost_price','>=',$cost_price_min];
            $where[] = ['p.cost_price','<=',$cost_price_max];
        }
        if(!empty($supply_price_min) && !empty($supply_price_max)){
            $where[] = ['p.supply_price','>=',$supply_price_min];
            $where[] = ['p.supply_price','<=',$supply_price_max];
        }

        $list = Product::productList($where,$page,$num);
        if(!empty($list)){
            $list = $list->toArray();
            foreach ($list as $key => $value) {
                $list[$key]["addtime"] = date( "Y-m-d H:i:s",$value["addtime"]);
                if(!empty($list[$key]['img'])){
                    $list[$key]['img'] =  $value['img'];
                }
                    /*if(strpos($value['img'],"img.alicdn.com") !== false){
                        $storeList[$key]["img"] =  $value['img'];
                    }
                    elseif(strpos($value['img'],"http") !== false){
                        $storeList[$key]["img"] =  $value['img'];
                    }
                    else{
                        
                        $storeList[$key]["img"] =  Config::get('app.swimming_url').$value['img'];
                    }*/
            }
        }
        $count = Product::productCount($where);
        return json(['code' => 1, 'msg' => lang(''), 'data' => $list, 'count' => $count]);
    }
    
    //商品添加
    public function productAdd(){

        $a_uid = Request::param('a_uid');
        $enable = Request::param('enable');
        $p_name = Request::param('p_name');
        $p_num = Request::param('p_num');
        $br_id = Request::param('br_id');
        $class_one = Request::param('class_one');
        $class_two = Request::param('class_two');
        $class_thr = Request::param('class_thr');
        $supplier = Request::param('supplier');
        $con_person = Request::param('con_person');
        $con_information = Request::param('con_information');
        $res_person = Request::param('res_person');
        $cost_price = Request::param('cost_price');
        $link = Request::param('link');
        $weight = Request::param('weight');
        $volume = Request::param('volume');
        $remarks = Request::param('remarks');
        $img = Request::param('img');
        $supply_price = Request::param('supply_price');
        
        if(empty($enable)){
            $enable = 1;
        }
        if($enable == -1){
            $enable  = 0;
        }
        
        if(empty($type)){
            $type = 1;
        }
        $time = time();
        $data = [
            'p_num' => $p_num,
            'p_name' => $p_name,
            'enable' => $enable,
            'br_id' => $br_id,
            'addtime' => $time,
            'type' => $type,
            'class_one' => $class_one,
            'class_two' => $class_two,
            'class_thr' => $class_thr,
            'supplier' => $supplier,
            'con_person' => $con_person,
            'con_information' => $con_information,
            'res_person' => $res_person,
            'cost_price' => $cost_price,
            'link' => $link,
            'supply_price' => $supply_price,
            'weight' => $weight,
            'volume' => $volume,
            'remarks' => $remarks,
            'img' => $img,
        ];

        Product::productAdd($data);
        return json(['code' => 1, 'msg' => lang(''), 'data' => '', 'count' => '']);
    }
    
    //商品详情
    public function productInfo(){
        $a_uid = Request::param('a_uid');

        $id = Request::param('id');
  
        $where = [];
        $where[] = ['p.id','=',$id];

        $info = Product::productInfo($where);
        if(!empty($info)){
            $info = $info->toArray();

            $info["addtime"] = date( "Y-m-d H:i:s",$info["addtime"]);
            if(!empty($info['img'])){
                $info['img'] =  $info['img'];
            }
            /*if(strpos($storeList['img'],"img.alicdn.com") !== false){
                $storeList["img"] =  $storeList['img'];
            }
            elseif(strpos($storeList['img'],"http") !== false){
                $storeList["img"] =  $storeList['img'];
            }
            else{

                $storeList["img"] =  Config::get('app.swimming_url').$storeList['img'];
            }*/
       
        }

        return json(['code' => 1, 'msg' => lang(''), 'data' => $info, 'count' => '' ]);
    }
    
    //商品修改
    public function productEdit(){
        $a_uid = Request::param('a_uid');
        $enable = Request::param('enable');
        $p_name = Request::param('p_name');
        $class_one = Request::param('class_one');
        $class_two = Request::param('class_two');
        $class_thr = Request::param('class_thr');
        $supplier = Request::param('supplier');
        $con_person = Request::param('con_person');
        $con_information = Request::param('con_information');
        $res_person = Request::param('res_person');
        $cost_price = Request::param('cost_price');
        $link = Request::param('link');
        $weight = Request::param('weight');
        $volume = Request::param('volume');
        $img = Request::param('img');
        $remarks = Request::param('remarks');
        $id = Request::param('id');
        $supply_price = Request::param('supply_price');
        
        $update = [];
        if(!empty($p_name)){
            $update['p_name'] = $p_name;
        }
        
        if(!empty($type)){
            $update['type'] = $type;
        }
        
        if(!empty($class_one)){
            $update['class_one'] = $class_one;
        }
        
        if(!empty($class_two)){
            $update['class_two'] = $class_two;
        }
        
        if(!empty($class_thr)){
            $update['class_thr'] = $class_thr;
        }
        
        /*if(!empty($supplier)){
            $update['address'] = $address;
        }*/
        if(!empty($supply_price)){
            $update['supply_price'] = $supply_price;
        }
        if(!empty($supplier)){
            $update['supplier'] = $supplier;
        }
        if(!empty($con_person)){
            $update['con_person'] = $con_person;
        }
        if(!empty($con_information)){
            $update['con_information'] = $con_information;
        }
        if(!empty($res_person)){
            $update['res_person'] = $res_person;
        }
        if(!empty($cost_price)){
            $update['cost_price'] = $cost_price;
        }
        if(!empty($link)){
            $update['link'] = $link;
        }
        if(!empty($weight)){
            $update['weight'] = $weight;
        }
        if(!empty($volume)){
            $update['volume'] = $volume;
        }
        if(!empty($res_person)){
            $update['res_person'] = $res_person;
        }
        if(!empty($remarks)){
            $update['remarks'] = $remarks;
        }
        if(!empty($img)){
            $update['img'] = $img;
        }
        if($enable==-1){
            $update['enable'] = 0;
        }else{
            $update['enable'] = 1;
        }

        Product::productEdit($id,$update);
        return json(['code' => 1, 'msg' => lang(''), 'data' => '', 'count' => '']);
    }
    
    //物料详情
    public function materialInfo(){
        $a_uid = Request::param('a_uid');

        $id = Request::param('id');
  
        $where = [];
        $where[] = ['id','=',$id];

        $info = Product::productInfo($where);
        if(!empty($info)){
            $info = $info->toArray();

            $info["addtime"] = date( "Y-m-d H:i:s",$info["addtime"]);
            if(is_array($info['img_json'])){
                foreach ($info['img_json'] as $k => $v) {
                    if(strpos($info['img_json'][$k],"img.alicdn.com") !== false){
        //                $listinfo[$key]['commodity']['picture_url'][$k] =  Config::get('app.pomegranate_url').$value['commodity']['picture'][$k];
                        $info['img_json'][$k] =  $info['img_json'][$k];
                    }
                    elseif(strpos($info['img_json'][$k],"http") !== false){
                        $info['img_json'][$k] =  $info['img_json'][$k];
                    }
                    else{
                        $info['img_json'][$k] =  $info['img_json'][$k];
                    }
                }
            }

        }

        return json(['code' => 1, 'msg' => lang(''), 'data' => $info, 'count' => '' ]);
    }
    
    //物料修改
    public function materialEdit(){
        $a_uid = Request::param('a_uid');
        $id = Request::param('id');
        
        $img_json = Request::param('img_json');
        $det_info = Request::param('det_info');
        $live_script = Request::param('live_script');
        if(!empty($img_json)){
            $update['img_json'] = $img_json;
        }
        if(!empty($det_info)){
            $update['det_info'] = $det_info;
        }
        if(!empty($live_script)){
            $update['live_script'] = $live_script;
        }
        
        Product::productEdit($id,$update);
        return json(['code' => 1, 'msg' => lang(''), 'data' => '', 'count' => '']);
    }

}
